'''
Created on 2011-08-25

@author: michael
'''
import datetime
import time

from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

class User(db.Model):
    #user info
    user_email = db.StringProperty()
    user_id = db.StringProperty()
    
    #date joined !!!
    date_joined = db.DateProperty()
    
    def labels(self):
        #returns all labels attached to user
        return self.user_labels

class Label(db.Model):
    
    #actual label title
    tag = db.StringProperty()
    
    #user's collection of labels
    user = db.ReferenceProperty(User, collection_name="user_labels")

class Receipt(db.Model):
    #dollars and cents. store using text parser
    dollars = db.IntegerProperty()
    cents = db.IntegerProperty()
    
    #date of receipt
    date = db.DateProperty()
    date_added = db.DateProperty(auto_now_add=True)
    
    #user's collection of receipts
    user = db.ReferenceProperty(User, collection_name="user_receipts")
    
    #labels attached to the receipt
    """I want to do this as a list property and filter      """
    """receipt results by those with the label key in their """
    """labels.ListProperty().                               """
    labels = db.ListProperty(db.Key)

def current_date():
    #produces Date object with current year, month, and day
    #datetime.datetime(YYYY, MM, DD)  
    t = time.localtime()
    date_frmtd = datetime.date(t[0], t[1], t[2])
    return date_frmtd

def frmt_date(s):
    #String -> Date
    #consumes s and produces date object from s
    pass

def make_account(email, id):
    #Str Str -> Key
    #side effect: creates and stores User object
    #creates user account and populates with default labels
    new_account = User(user_email = email,
                       user_id = id,
                       date_joined = current_date())
    new_account.put()
                       
    default_labels = ['fun', 'food', 'eat out', 'rent', 'school']
    for l in default_labels:
        Label(tag = l,
              user = new_account,
              parent = new_account.key()).put()
    
    return new_account.key()

def query_receipts_labels(usr_key, labels):
    #Key (Listof Label) => (Listof Receipt) !!!
    #takes labels & usr_key and produces user's all receipts with at least 1 of labels    
    pass

def query_receipts_labels_exact(usr_key, label_keys):
    #Key (Listof Label_Keys) -> Str
    #takes labels & usr_key and produces gql string with all labels
    #returns up to 30
    gql_str = "SELECT * FROM Receipt WHERE ANCESTOR IS %s" %(str(usr_key))
    
    for l in label_keys:
        gql_str += " AND labels = %s" %(str(l))
    
    return gql_str

def exact_search_receipts():
    pass

def all_labels(email, id):
    #str str -> (list of Label), (list of Keys)
    
    user = User.gql("WHERE user_email = :1 AND user_id = :2", email, id)
    r = user.get()
    labels = [l for l in r.user_labels]
    label_keys = [k.key() for k in r.user_labels]
    
    return labels, label_keys

def get_accnt(email, id):
    #str str -> User
    query = User.gql("WHERE user_email = :1 AND user_id = :2", email, id)
    result = query.get()
    return result

if __name__ == '__main__':
    pass